package com.bolehui.domain.po;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * @author chendongjie     email:chendongjie@gz.iscas.ac.cn
 * @version 1.0
 * @ClassName LoginPO
 * @Description
 * @createTime 2024/12/19 17:45
 * Copyright (C) 2021-2022 CASEEDER, All Rights Reserved.
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 */
@Data
@NoArgsConstructor
public class LoginPO implements UserDetails {

    private UserPO userPO;
    private Set<String> permissions;
    public LoginPO(UserPO userPO,  Set<String> permissions){
        this.userPO = userPO;
        this.permissions = permissions;
    }
    @JSONField(serialize = false)
    private Set<GrantedAuthority> authorities;
    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Set<GrantedAuthority> authorities = new HashSet<>();
        for (String permission : permissions) {
            // 将每个权限字符串转换为 GrantedAuthority 对象
            GrantedAuthority authority = new SimpleGrantedAuthority(permission);
            authorities.add(authority);
        }
        return authorities;
    }

    @Override
    @JsonIgnore
    public String getPassword() {
        return userPO.getPassword();
    }

    @Override
    @JsonIgnore
    public String getUsername() {
        return userPO.getUserName();
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return true;
    }
}
